package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.InternInfo;
import com.amsp.oa.oademo.mapper.InternInfoMapper;
import com.amsp.oa.oademo.service.InternInfoService;
import com.amsp.oa.oademo.vo.InternInfoVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class InternInfoServiceImpl extends ServiceImpl<InternInfoMapper, InternInfo> implements InternInfoService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateInternInfo(InternInfoVO vo) {
        InternInfo entity = new InternInfo();
        // 将items转换为JSON字符串
        if (vo.getItems() != null) {
            vo.setItemsJson(JSON.toJSONString(vo.getItems()));
        }
        BeanUtils.copyProperties(vo, entity);

        // 手动设置其他字段
        entity.setGender(vo.getGender());
        entity.setAge(vo.getAge());
        entity.setPoliticalStatus(vo.getPoliticalStatus());
        entity.setWorkExperience(vo.getWorkExperience());
        entity.setInternshipStartDate(vo.getInternshipStartDate());
        entity.setInternshipEndDate(vo.getInternshipEndDate());
        entity.setOpinion(vo.getOpinion());
        entity.setGroupLeaderApprover(vo.getGroupLeaderApprover());
        entity.setPresidentApprover(vo.getPresidentApprover());

        return this.saveOrUpdate(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchSaveOrUpdateInternInfo(List<InternInfoVO> voList) {
        List<InternInfo> entityList = voList.stream().map(vo -> {
            InternInfo entity = new InternInfo();
            if (vo.getItems() != null) {
                vo.setItemsJson(JSON.toJSONString(vo.getItems()));
            }
            BeanUtils.copyProperties(vo, entity);
            return entity;
        }).collect(Collectors.toList());
        return this.saveOrUpdateBatch(entityList);
    }

    @Override
    public InternInfoVO getInternInfoDetail(Long id) {
        InternInfo entity = this.getById(id);
        if (entity == null) {
            return null;
        }
        InternInfoVO vo = new InternInfoVO();
        BeanUtils.copyProperties(entity, vo);

        // 将JSON字符串转换回对象
        if (StringUtils.hasText(entity.getItemsJson())) {
            vo.setItems(JSON.parseArray(entity.getItemsJson(), InternInfoVO.Item.class));
        }

        return vo;
    }

    @Override
    public Page<InternInfoVO> getInternInfoPage(Page<InternInfo> page, InternInfoVO vo) {
        LambdaQueryWrapper<InternInfo> wrapper = new LambdaQueryWrapper<>();
        // 添加查询条件
        if (StringUtils.hasText(vo.getName())) {
            wrapper.like(InternInfo::getName, vo.getName());
        }
        if (StringUtils.hasText(vo.getEmployeeId())) {
            wrapper.like(InternInfo::getEmployeeId, vo.getEmployeeId());
        }
        if (StringUtils.hasText(vo.getDepartment())) {
            wrapper.like(InternInfo::getDepartment, vo.getDepartment());
        }

        Page<InternInfo> entityPage = this.page(page, wrapper);
        Page<InternInfoVO> voPage = new Page<>();
        BeanUtils.copyProperties(entityPage, voPage, "records");

        List<InternInfoVO> voList = entityPage.getRecords().stream().map(entity -> {
            InternInfoVO internInfoVO = new InternInfoVO();
            BeanUtils.copyProperties(entity, internInfoVO);

            // 将JSON字符串转换回对象
            if (StringUtils.hasText(entity.getItemsJson())) {
                internInfoVO.setItems(JSON.parseArray(entity.getItemsJson(), InternInfoVO.Item.class));
            }

            return internInfoVO;
        }).collect(Collectors.toList());
        voPage.setRecords(voList);

        return voPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteInternInfo(Long id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteInternInfo(List<Long> ids) {
        return this.removeByIds(ids);
    }
}